package eu.supersheep.dao;

import java.io.*;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;

import org.apache.http.*;
import org.apache.http.impl.client.*;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;

import android.util.Log;


public class HttpClient {
	private static final HttpClient sharedInstance = new HttpClient();
	
	private HttpClient() {
	}
	
    public static HttpClient getInstance() {
        return sharedInstance;
    }
    
    public String post (String url, Dictionary<String, String> params) {
        String response = "";
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        try {
        	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse execute = client.execute(httpPost);
            InputStream content = execute.getEntity().getContent();
            BufferedReader buffer = new BufferedReader( new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {
                    response += s;
            }
        } catch (Exception e) {
        	
        }
        Log.d("HTTP","req: "+url+" : "+response);
        
        return response;
    }
    
	public String get (String url) {
        String response = "";
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse execute = client.execute(httpGet);
            InputStream content = execute.getEntity().getContent();
            BufferedReader buffer = new BufferedReader( new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {
                    response += s;
            }
        } catch (Exception e) {
        	
        }
        Log.d("HTTP","req: "+url+" : "+response);
        
        return response;
	} 
	
}
